
package Control;

import Business.Product;
import Model.DAO;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Nguyen Hung
 */
public class CategoryServlet extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
       String url = "";
        HttpSession session = request.getSession();
        try {
            int pageNumber = 1;
            String pageNumberAsString = request.getParameter("pagenumber");
            if(pageNumberAsString == null)
                pageNumber = 1;
            else{
                pageNumber = Integer.parseInt(pageNumberAsString);
            }
            String vendor = request.getParameter("vendor");
            String keyword = request.getParameter("keyword");
            if (keyword == null) {
                keyword = "";
            }
            if (vendor == null) {
                vendor = "";
            }
            session.setAttribute("vendor", vendor);
            session.setAttribute("keyword", keyword);
            int numberOfRecords = getCount(vendor, keyword);
            ArrayList<Product>  products = getProducts(pageNumber, vendor, keyword);
            int pagen = numberOfRecords / 9;
            System.out.println(pagen);
            if((pagen % 9) != 0)
                pagen++;
            System.out.println(pagen);
            request.setAttribute("products", products);
            request.setAttribute("pagen", pagen);
            url="/category.jsp";
            request.setAttribute("products", products);
            if (products.isEmpty()) {             
                request.setAttribute("h1", "Không tìm thấy sản phẩm nào.");
            }
            url = "/category.jsp";
        } 
         catch (SQLException ex) {
            url = "/error.jsp";
        }

        RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(url);
        dispatcher.forward(request, response);
    }

    public static ArrayList<Product> getProducts(int pageNumber, String vendor, String keyword) throws SQLException {
        int offset = (pageNumber - 1) * 9;
        ArrayList<Product> result = new ArrayList<Product>();
        Connection connection = DAO.getConnection();
        String sql = "select *, vendorname from product, vendor where product.vendorid = vendor.vendorid and vendor.vendorname like ? and product.productCode like ? limit 9 offset " + offset;
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1, "%" + vendor + "%");
        preparedStatement.setString(2, "%" + keyword + "%");
        ResultSet rs = preparedStatement.executeQuery();
        while (rs.next()) {
            String productCode = rs.getString("productCode");
            String imgage = rs.getString("image");
            String cpu = rs.getString("cpu");
            String ram = rs.getString("ram");
            String hdd = rs.getString("hdd");
            String cardmonitor = rs.getString("cardmonitor");
            String display = rs.getString("display");
            String wireless = rs.getString("wireless");
            int warranty = rs.getInt("warranty");
            String battery = rs.getString("battery");
            String wightheight = rs.getString("weightheight");
            String description = rs.getString("description");
            String vendorName = rs.getString("vendorName");
            double price = rs.getDouble("price");
            String os = rs.getString("os");
            String DVDWL = rs.getString("DVD-WL");
            Product product = new Product(productCode, cpu, ram, hdd, cardmonitor, display, imgage, os, wireless, battery, wightheight, price, warranty, vendorName, DVDWL, description);
            result.add(product);

        }


        return result;
    }

    public int getCount(String vendor, String keyword) {
        int count = 0;
        Connection connection = DAO.getConnection();
        try {

            String sql = "select count(*) as count from product, vendor where product.vendorid = vendor.vendorid and vendor.vendorname like ? and product.productCode like ?";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, "%" + vendor + "%");
            preparedStatement.setString(2, "%" + keyword + "%");
            ResultSet set = preparedStatement.executeQuery();
            while (set.next()) {
                count = set.getInt("count");
            }
            connection.close();

        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return count;
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
